package stark.app.function.catdog;

import android.Manifest;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.catdog.lib.CatDogRecordPlayer;

import java.util.List;

import androidx.annotation.NonNull;
import stark.app.R;
import stark.app.databinding.ActivityCatDogDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;

/**
 * Author：daizhongbin on 2022/8/31
 * Email：1027154338@qq.com
 * Desc:
 */
public class CatDogDemoActivity extends BaseNoModelActivity<ActivityCatDogDemoBinding> implements View.OnTouchListener, CatDogRecordPlayer.IListener {

    private Handler mHandler = new Handler();
    private View mCurTouchView;
    private View mRecPlayView;
    private CatDogRecordPlayer mRecordPlayer;

    @Override
    protected int onCreate() {
        return R.layout.activity_cat_dog_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnHuman2Cat.setOnTouchListener(this);
        mDataBinding.btnCat2Human.setOnTouchListener(this);
        mDataBinding.btnHuman2Dog.setOnTouchListener(this);
        mDataBinding.btnDog2Human.setOnTouchListener(this);

        mDataBinding.btnShowCat.setOnClickListener(v -> {
            CatDogListActivity.showCat(this);
        });
        mDataBinding.btnShowDog.setOnClickListener(v -> {
            CatDogListActivity.showDog(this);
        });
    }

    @Override
    protected void initData() {
        initRecordPlayer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroyRecordPlayer();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

    }

    @Override
    public void onStateChanged(CatDogRecordPlayer.State state) {
        if (state == CatDogRecordPlayer.State.FINISH ||
                state == CatDogRecordPlayer.State.FINISH_FOR_TOO_SHORT) {
            if (state == CatDogRecordPlayer.State.FINISH_FOR_TOO_SHORT) {
                ToastUtils.showShort("录制时间太短");
            }
            updateCurTouchViewContent("开始录制");
            mRecPlayView = null;

        } else if (state == CatDogRecordPlayer.State.RECORDING) {
            mRecPlayView = mCurTouchView;
            updateCurTouchViewContent("正在录制中...");

        } else if (state == CatDogRecordPlayer.State.PLAYING) {
            updateCurTouchViewContent("正在播放中...");

        }
    }

    private Runnable taskStartRecord = new Runnable() {
        @Override
        public void run() {
            record(mCurTouchView);
        }
    };

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mRecordPlayer.stop();
                mCurTouchView = v;
                mHandler.removeCallbacks(taskStartRecord);
                mHandler.postDelayed(taskStartRecord, 500);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mHandler.removeCallbacks(taskStartRecord);
                mRecordPlayer.stopRec();
                break;
        }
        return true;
    }

    public void record(View view) {
        PermissionUtils.permission(Manifest.permission.RECORD_AUDIO)
                .callback(new PermissionUtils.SingleCallback() {
                    @Override
                    public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                        if (!isAllGranted) {
                            ToastUtils.showShort("请授予权限");
                            return;
                        }
                        CatDogRecordPlayer.Type type = CatDogRecordPlayer.Type.ANIMAL_2_HUMAN;
                        if (view == mDataBinding.btnHuman2Cat) {
                            type = CatDogRecordPlayer.Type.HUMAN_2_CAT;
                        } else if (view == mDataBinding.btnHuman2Dog) {
                            type = CatDogRecordPlayer.Type.HUMAN_2_DOG;
                        }
                        mRecordPlayer.startRec(type);
                    }
                })
                .request();
    }

    private void initRecordPlayer() {
        if (mRecordPlayer == null) {
            mRecordPlayer = CatDogRecordPlayer.getInstance();
            //可以设置最小录制时长，库内部默认为3秒
            //mRecordPlayer.setMinRecDuration(2000);
            mRecordPlayer.setListener(this);
        }
    }

    private void destroyRecordPlayer() {
        if (mRecordPlayer != null) {
            mRecordPlayer.release();
            mRecordPlayer = null;
        }
    }

    private void updateCurTouchViewContent(String text) {
        if (mRecPlayView == null) {
            return;
        }
        if (mRecPlayView instanceof TextView) {
            ((TextView) mRecPlayView).setText(text);
        }
    }

}
